package com.hbnu.Pojo;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport {

    private Integer totalNum;
    private Integer pageSize ;
    private Integer pageIndex ;
    private String submitUrl;


    @Override
    public int doEndTag() throws JspException {
        return super.doEndTag();
    }

    @Override
    public int doStartTag() throws JspException {
        JspWriter writer = pageContext.getOut();

        try {
            StringBuffer page = new StringBuffer();

            Integer totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;

            if (totalNum > pageSize) {
                if (pageIndex == 1) {
                    page.append("<a href='#'>首页</a>&nbsp;");
                    page.append("<a href='#'>上一页</a>&nbsp;");
                    page.append("<a href='").append(submitUrl).append("&pageIndex=").append(pageIndex + 1).append("'>下一页</a>&nbsp;");
                    page.append("<a href='").append(submitUrl).append("&pageIndex=").append(totalPage).append("'>尾页</a>&nbsp;");

                } else if (pageIndex.equals(totalPage)) {
                    page.append("<a href='").append(submitUrl).append("&pageIndex=").append(1).append("'>首页</a>&nbsp;");
                    page.append("<a href='").append(submitUrl).append("&pageIndex=").append(pageIndex - 1).append("'>上一页</a>&nbsp;");
                    page.append("<a href='#'>下一页</a>&nbsp;"); page.append("<a href='#'>尾页</a>&nbsp;");
                }
                else { // 当前页码在中间
                     page.append("<a href='").append(submitUrl).append("&pageIndex=").append(1).append("'>首页</a>&nbsp;");
                    page.append("<a href='").append(submitUrl).append("&pageIndex=").append(pageIndex - 1).append("'>上一页</a>&nbsp;");
                    page.append("<a href='").append(submitUrl).append("&pageIndex=").append(pageIndex + 1).append("'>下一页</a>&nbsp;");
                    page.append("<a href='").append(submitUrl).append("&pageIndex=").append(totalPage).append("'>尾页</a>&nbsp;");
                }
                page.append("<br> 当是第").append(pageIndex).append(" 页&nbsp;/共").append(totalPage).append("页 &nbsp;/共").append(totalNum).append("条数据");
            }
            writer.print(page.toString());
            writer.flush();
        }catch (Exception e) {
            e.printStackTrace();
            throw new JspException("分页标签生成出错：" + e.getMessage(), e);
        }

        return super.doStartTag();
    }

    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public void setSubmitUrl(String submitUrl) {
        this.submitUrl = submitUrl;
    }

    public Integer getTotalNum() {
        return totalNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public String getSubmitUrl() {
        return submitUrl;
    }
}
